//
//  YWAboutEwinViewController.m
//  maintain
//
//  Created by kyo on 17/3/15.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWAboutEwinViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"

#import "YWFeedBackViewController.h"
#import "YWWebViewController.h"

static CGFloat const padding = 15;
static CGFloat const headImaheSize = 75;
static CGFloat const versionLabelHeight = 21;
static CGFloat const footViewH = 60;

@interface YWAboutEwinViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView* headImageView;
@property (nonatomic, strong) UILabel* versionLabel;
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSArray* titleArray;
@property (nonatomic, strong) UIView* headView;
@property (nonatomic, strong) UIView* footView;
@end

@implementation YWAboutEwinViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"aboutEwin", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self autoLayoutView];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSArray*)titleArray{
    if (!_titleArray) {
        NSArray* arr = @[NSLocalizedString(@"feedBack", nil)];
        _titleArray = arr;
    }
    return _titleArray;
}

- (UIImageView*)headImageView{
    if (!_headImageView) {
        UIImageView* headIV = [[UIImageView alloc] init];
        headIV.image = [UIImage imageNamed:@"profile_logo"];
        headIV.layer.masksToBounds = YES;
        headIV.layer.cornerRadius = 10.0;
        _headImageView = headIV;
    }
    return _headImageView;
}

- (UILabel*)versionLabel{
    if (!_versionLabel) {
        UILabel* versionL = [[UILabel alloc] init];
        versionL.textAlignment = NSTextAlignmentCenter;
        NSString *ewin = NSLocalizedString(@"ewin", nil);
        versionL.text = [NSString stringWithFormat:@"%@ V%@",ewin, XcodeAppVersion];
        _versionLabel = versionL;
    }
    return _versionLabel;
}

- (UITableView*)tableView{
    if (!_tableView) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        tableView.separatorColor = [UIColor customLineColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        //tableView.tableHeaderView = self.headView;
        //tableView.tableFooterView = self.footView;
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

-(UIView *)headView{
    if (!_headView) {
        UIView* headView = [[UIView alloc] init];
        _headView = headView;
        [_headView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, versionLabelHeight + headImaheSize + padding + 5 + padding)];
        
        [headView addSubview:self.headImageView];
        [headView addSubview:self.versionLabel];
    }
    return _headView;
}

- (UIView*)footView{
    if (!_footView) {
        UIView* footView = [[UIView alloc] init];
        
        UIButton* protocolBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
        protocolBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [protocolBtn setTitle:NSLocalizedString(@"termsOfUseAndPrivacyPolicy", nil) forState:UIControlStateNormal];
        [protocolBtn setTitleColor:[UIColor customBlueColor] forState:UIControlStateNormal];
        [protocolBtn addTarget:self action:@selector(handleProtocolButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:protocolBtn];
        
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(protocolBtn.frame) + 5, CGRectGetWidth(protocolBtn.frame), 40)];
        label.font = [UIFont systemFontOfSize:11];
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        NSString *copyright = NSLocalizedString(@"copyright", nil);
        label.text = [NSString stringWithFormat:@"中科位到 %@\nCopyright 2017 ewin\nAll Rights Reserved",copyright];
        [footView addSubview:label];
        _footView = footView;
    }
    return _footView;
}

- (void)autoLayoutView{
    //自动布局
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.headView.mas_top).offset(padding+5);
        make.centerX.mas_equalTo(self.headView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(headImaheSize, headImaheSize));
    }];
    
    [self.versionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(padding);
        make.left.mas_equalTo(self.headView.mas_left);
        make.right.mas_equalTo(self.headView.mas_right);
        make.height.mas_equalTo(versionLabelHeight);
    }];
    
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    
    [self.footView setFrame:CGRectMake(0, self.tableView.frame.size.height - footViewH - padding, SCREEN_WIDTH, footViewH)];
    [self.tableView addSubview:self.footView];
}

#pragma mark - UITableViewDataSource mothods
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    static NSString *identifier = @"YWAboutEwinViewControllerCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        cell.textLabel.font = KTitleFont;
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark - UITableViewDataDelegate mothods
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    return self.headView.frame.size.height + padding;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return padding;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headView;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWFeedBackViewController* fbvc = [[YWFeedBackViewController alloc] init];
    [self.navigationController pushViewController:fbvc animated:YES];
}

- (void)handleProtocolButtonEvent{
    //隐私和条款
    YWWebViewController* webVC = [[YWWebViewController alloc] init];
    webVC.title = NSLocalizedString(@"protocol", nil);
    webVC.passURL = @"https://cdn.ew119.com/license/license.html";
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
